package com.mc.moring.main.modle

import com.mc.moring.app.LogUtils
import com.mc.moring.base.BaseModle
import com.mc.moring.base.HttpCallback
import com.mc.moring.netconfig.Apis
import com.mc.moring.network.HttpResult
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

class LoanModle : BaseModle() {

    private val coroutineScope = CoroutineScope(Dispatchers.Default)
    companion object {
        val instant by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            LoanModle()
        }
    }

    fun loancalLoan(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoanServer.loancalLoan(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun bankbindAccount(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoanServer.bankbindAccount(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }
    fun finishPersonalInfo(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoanServer.finishPersonalInfo(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun bankgetBankAccountList(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoanServer.bankgetBankAccountList(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun bankgetPayBankList(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody = mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoanServer.bankgetPayBankList(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun loanapplyLoan(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody = mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoanServer.loanapplyLoan(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun loangetLastUnpaidOffLoan(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody = mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoanServer.loangetLastUnpaidOffLoan(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    suspend fun getLastUnpaidOffLoan(map: MutableMap<String,String>) : HttpResult<String> {
        return try {
            var result = ""
            val mapString = getZiLiPageJsonNew(map, false)
            val request: RequestBody =
                mapString.toRequestBody("application/json".toMediaTypeOrNull())
            val data = Apis.apiLoanServer.loangetLastUnpaidOffLoan(request)
            result = decryptingZiLiPageData(data)
            LogUtils.logE("e ：$result")
            HttpResult.Success(result)
        } catch (e: Exception) {
            LogUtils.logE("e ：$e")
            HttpResult.Failure(e)
        }
    }

    fun getLoanSummaryByPaymentId(map: MutableMap<String,String>,httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJsonNew(map, false)
                val request: RequestBody = mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiLoanServer.getLoanSummaryByPaymentId(request)
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

}